package com.liuyfly.leetcode.study;

import com.liuyfly.leetcode.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author liuyangfang at 2022-08-24 16:21
 */
@Slf4j
public class MergeArray {
    public static int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt(a->a[0]));
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < intervals.length - 1; i++) {
            if (list.contains(i)) {
                continue;
            }
            for (int j = i + 1; j < intervals.length; j++) {
                if (list.contains(j)) {
                    continue;
                }
                if (intervals[i][0] <= intervals[j][0]) {
                    if (intervals[i][1] >= intervals[j][0]) {
                        if (intervals[i][1] < intervals[j][1]) {
                            intervals[i][1] = intervals[j][1];
                        }
                        list.add(j);
                    }
                } else if (intervals[i][0] <= intervals[j][1]) {
                    intervals[i][0] = intervals[j][0];
                    if (intervals[i][1] >= intervals[j][0] && intervals[i][1] < intervals[j][1]) {
                        intervals[i][1] = intervals[j][1];
                    }
                    list.add(j);
                } else if (intervals[i][0] == intervals[j][1]) {
                    intervals[i][0] = intervals[j][0];
                    list.add(j);
                }
            }
        }
        int[][] result = new int[intervals.length - list.size()][intervals[0].length];
        int j = 0;
        for (int i = 0; i < intervals.length; i++) {
            if (list.contains(i)) {
                continue;
            }
            result[j] = intervals[i];
            j++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] array1 = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } };
        log.info("{}", JsonUtil.toJson(array1));
        int[][] result1 = merge(array1);
        log.info("{}", JsonUtil.toJson(result1));
        log.info("############################################################");
        int[][] array2 = { { 4, 5 }, { 2, 3 } };
        log.info("{}", JsonUtil.toJson(array2));
        int[][] result2 = merge(array2);
        log.info("{}", JsonUtil.toJson(result2));
        log.info("############################################################");
        int[][] array3 = { { 1, 5 }, { 6, 7 } };
        log.info("{}", JsonUtil.toJson(array3));
        int[][] result3 = merge(array3);
        log.info("{}", JsonUtil.toJson(result3));
        log.info("############################################################");
        int[][] array4 = { { 1, 5 }, { 0, 0 } };
        log.info("{}", JsonUtil.toJson(array4));
        int[][] result4 = merge(array4);
        log.info("{}", JsonUtil.toJson(result4));
        log.info("############################################################");
        int[][] array5 = { { 2, 3 }, { 4, 5 }, { 6, 7 }, { 8, 9 }, { 1, 10 } };
        log.info("{}", JsonUtil.toJson(array5));
        int[][] result5 = merge(array5);
        log.info("{}", JsonUtil.toJson(result5));
        log.info("############################################################");
        int[][] array = { { 2, 3 }, { 4, 6 }, { 5, 7 }, { 3, 4 } };
        log.info("{}", JsonUtil.toJson(array));
        int[][] result = merge(array);
        log.info("{}", JsonUtil.toJson(result));
        log.info("############################################################");
    }
}
